﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using MetaSharp.Transformation.Properties;
using System.Threading.Tasks;

namespace MetaSharp.Transformation.Tests.Patterns
{
	public class AndTests
	{
		[Fact]
		public async Task ReadsZeroPatternSuccessfully()
		{
			var p = Pattern.And();
            var m = await Pattern.Match(p, "");
			Assert.Empty((object[])m.Value);
		}

		[Fact]
		public async Task ReadsOnePatternSuccessfully()
		{
			var p = Pattern.And(Pattern.Any());
            var m = await Pattern.Match(p, "a");

            var values = (object[])m.Value;
            Assert.Single(values, 'a');
		}

		[Fact]
		public async Task ReadsTwoPatternsSuccessfully()
		{
			var p = Pattern.And(
				Pattern.Any(),
				Pattern.Any());
            var m = await Pattern.Match(p, "ab");
            var values = (object[])m.Value;
            Assert.Equal(new object[] { 'a', 'b' }, values);
		}

		[Fact]
		public async Task ReadsThreePatternsSuccessfully()
		{
			var p = Pattern.And(
				Pattern.Any(),
				Pattern.Any(),
				Pattern.Any());
            var m = await Pattern.Match(p, "abc");
            var values = (object[])m.Value;
            Assert.Equal(new object[] { 'a', 'b', 'c' }, values);
        }

		[Fact]
		public async Task DoesNotReadTooManyPatterns()
		{
            var p = Pattern.And(Pattern.Any());
            var m = await Pattern.Match(p, "ab");
            Assert.Equal(1, m.End.Stream.Index);
		}

        [Fact]
        public async Task AndPatternPropagatesFailsProperly()
        {
            var p = Pattern.And(Pattern.Any());
            var m = await Pattern.Match(p, "");
            Assert.False(m.Matched);
        }

        [Fact]
        public async Task AndPatternFailsIfThereIsntEnoughInput()
        { 
			var p = Pattern.And(
				Pattern.Any(),
				Pattern.Any());
            var m = await Pattern.Match(p, "a");

			Assert.False(m.Matched);
		} 
    }
}
